﻿using EasyXaf.EntityDesigner.BusinessObjects;
using EasyXaf.EntityDesigner.BusinessObjects.Entities.EntityProperties;

namespace EasyXaf.EntityDesigner.Commands.PropertyTypeBuilders;

public class EnumTypeBuilder
{
    public static bool Build(EntityProperty property, string typeName)
    {
        bool isNullable = false;

        if (typeName.EndsWith('?'))
        {
            isNullable = true;
            typeName = typeName[..^1];
        }
        else if (typeName.EndsWith('!'))
        {
            typeName = typeName[..^1];
        }

        var enumeration = property.Entity.DomainModel.AllEnums.FirstOrDefault(e => e.Name == typeName);
        if (enumeration != null)
        {
            property.PropertyType = PropertyType.Enumeration;
            property.EnumType = enumeration;
            if (isNullable)
            {
                property.IsNullable = DefaultBoolean.True;
            }
            return true;
        }

        return false;
    }
}
